package com.sevenit.nj.yrmtfish.entity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ServerData {
	
	//数据状态
	private int status = 0;
	//单个数据
	private Map<String, Object> map = null;
	//数据组
	private List<Map<String, Object>>list = null;
	//数据组总长度
    private int iAllCount = -1;
    //当前位置
    private int iCurrent = -1;
	
    /**
     * 
     */
    public ServerData() {
    }
    
    /**
     * 设置数据状态.
     * @return
     */
    public void setStatus(int iSta) {
    	this.status = iSta;
    }
    
    /**
     * 获取数据状态.
     * @return
     */
    public int getStatus() {
    	return this.status;
    }
    
    /**
     * 添加一个ITEM.
     * @return
     */
    public void newItem() {
    	if (this.list == null) {
            this.list = new ArrayList<Map<String, Object>>();
        }
    	this.map = new HashMap<String, Object>();
    }
    
    /**
     * 在ITEM理添加一个值对
     * @param strKey
     * @param strValue
     */
    public void addString(String strKey, String strValue) {
        if (this.map != null) {
            this.map.put(strKey, strValue);
        }
    }
    
    /**
     * 添加完成一个ITEM
     * @param strKey
     * @param strValue
     */
    public void closeItem() {
    	if (this.list != null && this.map != null) {
            this.list.add(this.map);
            this.map = null;
        }
    }
    
    /**
     * 取得总条数
     * 
     */
    private void getAllCount() {
    	if (this.list != null) {
            this.iAllCount = this.list.size();
        }
    }
    
    /**
     * 
     * @return
     */
    public int count() {
    	getAllCount();
    	return this.iAllCount;
    }
    
    /**
     * 
     * @return
     */
    public boolean first() {
    	getAllCount();
    	if (this.iAllCount <= 0) {
            return false;
        }
    	this.iCurrent = 0;
    	return true;
    }
    
    /**
     * 
     * @return
     */
    public boolean last() {
    	getAllCount();
    	if (this.iAllCount <= 0) {
            return false;
        }
    	this.iCurrent = this.iAllCount - 1;
    	return true;
    }
    
    /**
     * 
     * @return
     */
    public boolean next() {
    	getAllCount();
    	if (this.iAllCount <= 0 || this.iCurrent >= (this.iAllCount-1)) {
            return false;
        }
    	this.iCurrent += 1;
    	return true;
    }
    
    /**
     * 
     * @return
     */
    public boolean previous() {
    	getAllCount();
    	if (this.iAllCount <= 0 || this.iCurrent <= 0) {
            return false;
        }
    	this.iCurrent -= 1;
    	return true;
    }
    
    /**
     * 
     * @return
     */
    public boolean del() {
    	if (this.iAllCount <= 0 || this.iCurrent < 0){
    		return false;
        }
    	this.list.remove(this.iCurrent);
    	previous();
    	return true;
    }
    
    /**
     * 
     * @return
     */
    public String getString(String strKey) {
    	
    	String strValue = null;
    	if (this.iAllCount <= 0 || this.iCurrent < 0){
    		return null;
        }
    	
    	Map<String, Object> mpaT = this.list.get(this.iCurrent);
    	if(mpaT.containsKey(strKey)){
    		strValue = (String)mpaT.get(strKey);
    	}
    	
    	return strValue;
    }
}